import {Injectable} from '@angular/core';
import {Http, Headers, RequestOptions, RequestOptionsArgs} from '@angular/http';
import {adSystemApiUrl, tpType} from '../../../config/api';
import {HttpService} from '../../lib/service/http.service';

@Injectable()
export class CouponService {

  constructor(private httpService: HttpService) {

  }

  // 获取卡券列表
  getCouponList(page: number,
                pageSize: number,
                keyword: string) {
    const url = `${adSystemApiUrl}/v2/member/card_coupons/list`;
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers});
    const body = JSON.stringify({
      page: page,
      pageSize: pageSize,
      keyword: keyword
    });
    return this.httpService.httpPostAuth(url, body, options).map(rs => rs);
  }

  //下载图片
  downloadImage(src) {
      const url = `${adSystemApiUrl}/v2/member/card_coupons/download_share_pic?file_path_url=${src}`;
      return this.httpService.httpGetAuth(url).map(rs => rs);
  }

  // 获取Tag列表页
  getTagList(name) {
    const url = `${adSystemApiUrl}/v2/member/card_coupons_tag/get_all`;
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers});
    const body = {name: name};
    return this.httpService.httpPostAuth(url, body, options).map(rs => rs);
  }

  // 通过Tag获取卡券类型
  getCouponListByTag(card_coupons_tag_uuid) {
    const url = `${adSystemApiUrl}/v2/member/card_coupons/get_card_coupons_list_by_tag`;
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers});
    const body = JSON.stringify({card_coupons_tag_uuid: card_coupons_tag_uuid});
    return this.httpService.httpPostAuth(url, body, options).map(rs => rs);
  }

  // 删除卡券
  delCoupon(id: number) {
    const url = `${adSystemApiUrl}/v2/member/card_coupons/${id}/del`;
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers});
    const body = JSON.stringify({});
    return this.httpService.httpPostAuth(url, body, options).map(rs => rs);
  }

  // 删除卡券Tag
  delCardTag(card_coupons_uuid, card_coupons_tag_uuid) {
    const url = `${adSystemApiUrl}/v2/member/card_coupons/card_coupons_tag_unbinding`;
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers});
    const body = JSON.stringify({
      card_coupons_uuid: card_coupons_uuid,
      card_coupons_tag_uuid: card_coupons_tag_uuid
    });
    return this.httpService.httpPostAuth(url, body, options).map(rs => rs);
  }

  // 批量绑定卡券Tag
  batchCardBinding(card_coupons_tag_uuid, card_coupons_uuid_json) {
    const url = `${adSystemApiUrl}/v2/member/card_coupons/batch_card_coupons_tag_binding`;
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers});
    const body = JSON.stringify({
      card_coupons_tag_uuid: card_coupons_tag_uuid,
      card_coupons_uuid_json: JSON.stringify(card_coupons_uuid_json)
    });
    return this.httpService.httpPostAuth(url, body, options).map(rs => rs);
  }

  // 获取店铺商品类型列表
  getGroupList() {
    const url = `${adSystemApiUrl}/v1/member/applet_goods_category/list`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }

  // 获取商品列表
  getGoodsList(page: number,
               pageSize: number,
               applet_goods_category_id?: number,
               keyword?: string,
               status?:string
               ) {
    const _applet_goods_category_id = applet_goods_category_id || '',
      _keyword = keyword || '';
    const url = `${adSystemApiUrl}/v1/member/goods/list` +
      `?applet_goods_category_id=${_applet_goods_category_id}` +
      `&status=${status}` +
      `&keyword=${_keyword}&page=${page}&pageSize=${pageSize}`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }

  // 新增卡券
  addCoupon(data) {
    const url = `${adSystemApiUrl}/v2/member/card_coupons/add`;
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers});
    const body = JSON.stringify(data);
    return this.httpService.httpPostAuth(url, body, options).map(rs => rs);
  }

  // 编辑卡券
  editCoupon(id, condition_receive_quantity, relation_id_json) {
    const url = `${adSystemApiUrl}/v2/member/card_coupons/${id}/edit_part`;
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers});
    const body = JSON.stringify({
      condition_receive_quantity: condition_receive_quantity,
      relation_id_json: relation_id_json
    });
    return this.httpService.httpPostAuth(url, body, options).map(rs => rs);
  }

  // 获取卡券详情
  getCouponDetail(id) {
    const url = `${adSystemApiUrl}/v2/member/card_coupons/${id}/info`;
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers});
    const body = JSON.stringify({});
    return this.httpService.httpPostAuth(url, body, options).map(rs => rs);
  }

  // 获取商品详情
  getGoodsDetail(id) {
    const url = `${adSystemApiUrl}/v1/applet/goods/get_goods_info?id=${id}`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }

  // 获取小程序详情
  getAppletInfo() {
    const url = `${adSystemApiUrl}/v1/member/applets/applet_detail`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }

  // 申请开通卡券
  applyCoupon(appletName, applet_uuid) {
    const url = `${adSystemApiUrl}/v2/member/account/apply_merchant_key_post`;
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers});
    const body = JSON.stringify({
      name: appletName,
      tp_id: applet_uuid,
      tp_type: tpType
    });
    return this.httpService.httpPostAuth(url, body, options).map(rs => rs);
  }

  //生成优惠券二维码
  createCoupon(data) {
    const url = `${adSystemApiUrl}/v2/member/card_coupons/get_coupon_qr_code`;
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers});
    const body = JSON.stringify(data);
    return this.httpService.httpPostAuth(url, body, options).map(rs => rs);
  }

  //获取优惠券图片
    getCouponImage(data) {
        const url = `${adSystemApiUrl}/v2/member/card_coupons/get_share_pic`;
        const headers = new Headers({'Content-Type': 'application/json'});
        const options = new RequestOptions({headers: headers});
        const body = JSON.stringify(data);
        return this.httpService.httpPostAuth(url, body, options).map(rs => rs);
    }
}
